package pers.qianyu.month_202102.date_20210204;

/**
 * 643. 子数组最大平均数 I
 * https://leetcode-cn.com/problems/maximum-average-subarray-i/
 *
 * @author mizzle rain
 * @date 2021-02-04 20:40
 */
public class FindMaxAverage {
    public double findMaxAverage(int[] nums, int k) {
        int len = nums.length;
        if (len == 0) return 0;
        int sum = 0, max = Integer.MIN_VALUE;
        for (int i = 0; i < len; i++) {
            sum += nums[i];
            if (i >= k - 1) {
                if (i >= k) sum -= nums[i - k];
                max = Math.max(max, sum);
            }
        }
        return ((double) max) / k;
    }
}
